<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="../include/laypublic.jsp" %>
<!DOCTYPE html>
<html>
<head>
</head>
<title>授信余额管理</title>
<style type="text/css">
    .layui-form-select dl {
        max-height: 150px;
    / / 原值为300，调节高度建议为50的倍数
    }

    .layui-form-label {
        width: 130px;
    }

    .layui-form-item {
        margin-bottom: 0px;
        clear: both;
        *zoom: 1;
    }
</style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <!-- 查询区域  -->
        <div id="query-form" class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">授信单位</label>
                    <div class="layui-input-inline">
                        <input type="text" name="creditUnits" placeholder="请输入授信单位" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">日期</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="sent-laydate-start" name="addTime"
                               placeholder="请选择创建时间">
                    </div>
                </div>
                <shiro:hasPermission name="LIST_LOANFIN">
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-loanFin-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                        </button>
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-reset">
                            <i class="layui-icon layui-icon-fonts-clear  layuiadmin-button-btn"></i>重置
                        </button>
                    </div>
                </shiro:hasPermission>
                <div class="layui-inline">
                    <shiro:hasPermission name="ADD_LOANFIN">
                        <button class="layui-btn layuiadmin-btn-admin " data-type="add">
                            <i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>新建
                        </button>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="EXPORT_LOANFIN">
                        <button class="layui-btn layuiadmin-btn-admin " data-type="export" onclick="exportList();">
                            <i class="layui-icon layui-icon-export layuiadmin-button-btn"></i>导出
                        </button>
                    </shiro:hasPermission>
                </div>
            </div>
        </div>
        <!-- 按钮区域  -->
        <div class="layui-card-body">
            <!-- 数据表单 -->
            <div class="layui-tab-item layui-show ">
                <table id="LAY-loanFin-wait-manage" lay-filter="LAY-loanFin-wait-manage"></table>
                <div id="table-foot"></div>
            </div>
        </div>
    </div>
</div>

<form class="layui-form" lay-filter="add_line_form" id="add_line_form" style="padding: 20px 30px 0 0;display:none">
    <input type="hidden" name="id" value="${baseLine.id}"/>
    <div class="layui-form-item" id="linkRow1">
        <div class="layui-form-item" id="creditUnits">
            <div class="layui-inline">
                <label class="layui-form-label">授信单位:</label>
                <div class="layui-input-inline" style="width: 360px;">
                    <input type="text" name="creditUnits" placeholder="请输入银行名称" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" id="creditLines">
            <div class="layui-inline">
                <label class="layui-form-label">授信额度:</label>
                <div class="layui-input-inline" style="width: 360px;">
                    <input type="text" name="creditLines" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" id="lineListHtml">
            <div class="layui-inline">
                <label class="layui-form-label">银行状态:</label>
                <div class="layui-input-inline" style="width: 360px;">
                    <input type="radio" name="status" value="0" title="不可授信">
                    <input type="radio" name="status" value="1" title="可授信">
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <input type="button" lay-submit lay-filter="add_line_form_submit" id="add_line_form_submit" value="确认">
        </div>
    </div>
</form>

<script>
    layui.config({
        base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'set', 'transfer', 'laydate'], function () {
            var $ = layui.$
                , form = layui.form
                , table = layui.table
                , element = layui.element
                , laypage = layui.laypage
                , laydate = layui.laydate;
            //tab重新渲染宽度
            element.on('tab(component-tabs-brief)', function (obj) {
                $(window).resize();
            });
            var dataSS = []; //列表集合
            var statusCols = []; //表头集合
            var perPage = 10;
            var nowPage = 1;
            var firstReqData = {page: nowPage, limit: perPage};
            tableInital(nowPage, perPage, firstReqData, true); //初始化加载


            function tableInital(pageNum, pageSize, reqData, first) {
                var $ = layui.$;
                $.ajax({
                    type: 'post',
                    url: basePath + '/creditBalance/queryDataByCondition',  //请求数据接口
                    async: false,
                    data: reqData,
                    success: function (result) {
                        if (!result.success) {
                            layer.msg(result.msg, {icon: 7});
                            return;
                        }
                        if (result.data.length < 1) {
                            dataSS = [];
                            statusCols = [];
                        } else {
                            //初始化加载表格渲染和分页查询
                            tableTitleAndShow(result.data);
                        }
                        table.render({
                            elem: '#LAY-loanFin-wait-manage',
                            cols: [statusCols],
                            data: dataSS,
                            done: function (res, curr, count) {
                            }
                        });
                        if (first) tablePage(pageNum, result.count);

                        table.reload('LAY-loanFin-wait-manage'); //数据刷新
                    }
                });
            }

            //分页请求重写
            function tablePage(curr, count) {
                console.info(curr, count, laypage);
                laypage.render({
                    elem: 'table-foot',
                    count,
                    curr,
                    limit: perPage,
                    jump: function (obj, first) {
                        nowPage = obj.curr;
                        if (!first) {
                            var reqData = {page: nowPage, limit: perPage};
                            tableInital(nowPage, perPage, reqData)
                        }
                    }
                });
            }

            // 通过后台参数生成自动列表并渲染
            function tableTitleAndShow(data) {
                console.log(data);
                var dataFiled = []; //表头集合
                dataSS = [];
                statusCols = [];
                dataSS = data;
                statusCols[0] = {type: 'checkbox', fixed: 'left', width: 50};
                statusCols[1] = {field: 'creditUnits', title: '授信单位', width: 170, align: 'center'};
                statusCols[2] = {field: 'creditLines', title: '授信额度', width: 280, align: 'center'};
                statusCols[3] = {field: 'usedCreditLines', title: '已用授信额度', width: 170, align: 'center'};
                statusCols[4] = {field: 'allotableCreditLines', title: '可分配授信额度', width: 170, align: 'center'};
                statusCols[5] = {
                    field: 'status', title: '状态', width: 170, align: 'center', templet: function (d) {
                        if (d.status == '0') {
                            return '当前不可授信';
                        } else if (d.status == '1') {
                            return '当前可授信';
                        }
                    }
                };
                statusCols[6] = {
                    field: 'uptTime', title: '更新时间', width: 170, align: 'center', templet: function (d) {
                        return layui.util.toDateString(d.uptTime, "yyyy-MM-dd");
                    }
                };
                statusCols[7] = {
                    title: '操作', width: 230, align: 'center', fixed: 'right', align: 'center', templet: function (d) {
                        var optStr = "";
                        <shiro:hasPermission name="VIEW_LOANFIN">
                        optStr += '<a class="layui-btn layui-btn-xs" lay-event="view"><i class="layui-icon layui-icon-form"></i>详情</a>';
                        </shiro:hasPermission>
                        if (d.processStatus == '10') {
                            <shiro:hasPermission name="COPY_LOANFIN">
                            optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="copy"><i class="layui-icon layui-icon-release"></i>复制单据</a>';
                            </shiro:hasPermission>
                            optStr += '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                        }
                        return optStr;
                    }
                }
            }

            //开始日期
            var insStart = laydate.render({
                elem: '#sent-laydate-start'
                , done: function (value, date) {
                    //更新结束日期的最小日期
                    insEnd.config.min = lay.extend({}, date, {
                        month: date.month - 1
                    });

                    //自动弹出结束日期的选择器
                    insEnd.config.elem[0].focus();
                }
            });

            //结束日期
            var insEnd = laydate.render({
                elem: '#sent-laydate-end'
                , done: function (value, date) {
                    //更新开始日期的最大日期
                    insStart.config.max = lay.extend({}, date, {
                        month: date.month - 1
                    });
                }
            });

            //放款开始日期
            var insLoanStart = laydate.render({
                elem: '#sent-layLoandate-start'
                , done: function (value, date) {
                    //更新结束日期的最小日期
                    insLoanEnd.config.min = lay.extend({}, date, {
                        month: date.month - 1
                    });

                    //自动弹出结束日期的选择器
                    insLoanEnd.config.elem[0].focus();
                }
            });

            //放款结束日期
            var insLoanEnd = laydate.render({
                elem: '#sent-layLoandate-end'
                , done: function (value, date) {
                    //更新开始日期的最大日期
                    insLoanStart.config.max = lay.extend({}, date, {
                        month: date.month - 1
                    });
                }
            });
            // 正则校验
            form.verify({
                //验证汉字
                CHS: [
                    /^[\u0391-\uFFE5]+$/
                    , '只能输入汉字'
                ],
                //大于0的正整数
                intNumber: [
                    /^[1-9]\d*$/
                    , '请输入正确的数字'
                ]
            });

            <shiro:hasPermission name="LIST_LOANFIN">
            //监听搜索
            form.on('submit(LAY-loanFin-search)', function (data) {
                var reqData = trimData(data);
                reqData.page = nowPage;
                reqData.limit = perPage;
                tableInital(nowPage, perPage, reqData, true);
            });
            //重置清空
            form.on('submit(LAY-user-back-reset)', function (data) {
                reset_form('query-form');
                //额外处理select
                $("#query-form-state").val('');
                form.render();
            });
            </shiro:hasPermission>

            // 监听操作列
            table.on('tool(LAY-loanFin-create-manage)', function (obj) {
                var data = obj.data;
                if (obj.event === 'view') {
                    parent.layui.index.openTabsPage(basePath + '/loanFin/toDealOrDetail?id=' + data.id, '融资放款 > 详情');
                } else if (obj.event === 'copy') {//复制单据
                    parent.layui.index.openTabsPage(basePath + '/loanFin/toCopy?id=' + data.id + '&proCode=' + data.proCode, '融资放款 > 复制单据');
                } else if (obj.event === 'del') {//删除
                    layer.confirm('确定要删除？', {icon: 3, title: '提示'}, function (index) {
                        $.ajax({
                            type: 'post',
                            url: basePath + '/flowProcess/logicDel',
                            data: {
                                "processId": data.processId
                            },//数据，这里使用的是Json格式进行传输
                            success: function (result) {
                                if (result) {
                                    table.reload('LAY-loanFin-create-manage'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.msg('数据通信异常~', {icon: 0});
                                }
                            }
                        });
                    })
                }
            });

            // 监听操作列
            table.on('tool(LAY-loanFin-wait-manage)', function (obj) {
                var data = obj.data;
                if (obj.event === 'view') {
                    parent.layui.index.openTabsPage(basePath + '/loanFin/toDealOrDetail?id=' + data.id, '融资放款 > 详情');
                } else if (obj.event === 'toDispatch') {
                    parent.layui.index.openTabsPage(basePath + '/flowProcess/toDispatch?processId=' + data.processId, '融资放款 ' + ' > 审批');
                }
            });

            // 弹出层通用方法
            function openTableLayui(baselineId, method) {
                console.log("打开弹出层" + baselineId);
                var $ = layui.$;
                var methodTitle;
                if (method == "edit") {
                    methodTitle = "修改";
                } else if (method == "view") {
                    methodTitle = "查看";
                } else if (method == "copy") {
                    methodTitle = "复制";
                } else if (method == "add") {
                    $('#select_city').html('');
                    $('#select_area').html('');
                    methodTitle = "新增";
                }
                var maxInd = $("div[id^='linkRow']").length;
                $("div[id^='linkRow']").each(function () {
                    if ($(this).attr("id") != "linkRow1") {
                        $(this).remove();
                    }
                });

                layer.open({
                    type: 1
                    , title: methodTitle + '授信'
                    , content: $('#add_line_form')
                    , area: layui.admin.screen() < 2 ? ['80%', '450px'] : ['600px', '450px']
                    , btn: ['保存', '取消']
                    , btnAlign: 'c'
                    , yes: function (index, layero) {
                        var submitID = 'add_line_form_submit'
                            , submit = layero.contents().find('#' + submitID);

                        submitFormAjax(method, index, function () {
                            var reqData = {page: nowPage, limit: perPage};
                            tableInital(nowPage, perPage, reqData);
                        });

                        submit.trigger('click');
                    }
                });
            }

            function cleanForm() {
                var $ = layui.$;
                $('input[name=creditUnits]').val('');
                $('input[name=addTime]').val('');
                form.render('select');
                clean_form("add_line_form");//清空表单
            }

            //提交 Ajax 成功后，静态更新表格中的数据
            function submitFormAjax(method, openTableId, cb) {
                var $ = layui.$;
                $.ajax({
                    type: 'post',   //+ method,
                    url: basePath + '/creditBalance/add',
                    data: $("#add_line_form").serialize(),
                    success: function (result) {
                        if (result.success) {
                            clean_form("add_line_form");//清空表单
                            layer.close(openTableId); //关闭弹层
                            if (cb) cb();
                        } else {
                            layer.msg(result.msg, {icon: 7});
                        }
                    }
                });
            }

            // 按钮事件
            var active = {
                add: function () {
                    //parent.layui.index.openTabsPage(basePath + '/loanFin/add','融资放款 > 新建');
                    openTableLayui(null, 'add');
                    cleanForm();
                }
            }
            // data-type事件
            $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //导出excel
            function exportList() {
                layer.msg("暂未上线，敬请期待~", {icon: 1});
            }
        }
    );
</script>
</body>
</html>

